﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
namespace DAO
{
    public static class DataProvider
    {
        private static OleDbConnection _con = null;

        static DataProvider()
        {
            string strConnection = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = CSDL_TKB.mdb";
            _con = new OleDbConnection(strConnection);
        }

        // thực thi câu truy vấn sql
        public static DataTable ExecuteQuery(string strQuery)
        {
            DataTable resTable = new DataTable();
            try
            {
                _con.Open();
                OleDbDataAdapter adapter = new OleDbDataAdapter(strQuery, _con);
                adapter.Fill(resTable);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi thực thi lệnh SQL: " + ex.Message);
            }
            finally
            {
                _con.Close();
            }
            return resTable;
        }

        // thực thi câu truy vấn sql
        public static DataTable ExecuteNonQuery(string strQuery, IList<OleDbParameter> arrParameter)
        {
            DataTable resTable = new DataTable();
            try
            {
                _con.Open();
                OleDbCommand cmd = new OleDbCommand(strQuery, _con);
                int N = arrParameter.Count;
                for (int i = 0; i < N; i++)
                {
                    cmd.Parameters.Add(arrParameter[i]);
                }
                OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                adapter.Fill(resTable);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi thực thi lệnh SQL: " + ex.Message);
            }
            finally
            {
                _con.Close();
            }
            return resTable;
        }
    }
}
